import 'package:shared_preferences/shared_preferences.dart';

class PreferenceUtils {
  static SharedPreferences? _sp;

  static Future<void> init() async {
    _sp = await SharedPreferences.getInstance();
  }

  static Future<void> _checkInit() async {
    if (_sp == null) {
      await init();
    }
  }

  static Future<bool> setString(String key, String value) async {
    await _checkInit();
    return _sp!.setString(key, value);
  }

  static String getString(String key) {
    if (_sp == null) return "";
    return _sp!.getString(key) ?? "";
  }

  static Future<bool> setBool(String key, bool value) async {
    await _checkInit();
    return _sp!.setBool(key, value);
  }

  static bool getBool(String key) {
    if (_sp == null) return false;
    return _sp!.getBool(key) ?? false;
  }

  static Future<bool> setInt(String key, int value) async {
    await _checkInit();
    return _sp!.setInt(key, value);
  }

  static int getInt(String key) {
    if (_sp == null) return 0;
    return _sp!.getInt(key) ?? 0;
  }

  static Future<bool> setDouble(String key, double value) async {
    await _checkInit();
    return _sp!.setDouble(key, value);
  }

  static double getDouble(String key) {
    if (_sp == null) return 0.0;
    return _sp!.getDouble(key) ?? 0.0;
  }

  static Future<bool> setList(String key, List<String> value) async {
    await _checkInit();
    return _sp!.setStringList(key, value);
  }

  static List<String> getList(String key) {
    if (_sp == null) return [];
    return _sp!.getStringList(key) ?? [];
  }

  static Future<bool> remove(String key) async {
    await _checkInit();
    return _sp!.remove(key);
  }

  static Future<bool> clear() async {
    await _checkInit();
    return _sp!.clear();
  }
}
